
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
 */
package supermarketgui;

//~--- non-JDK imports --------------------------------------------------------

import org.netbeans.validation.api.ValidatorUtils;
import org.netbeans.validation.api.builtin.stringvalidation.StringValidators;
import org.netbeans.validation.api.ui.ValidationGroup;
import org.netbeans.validation.api.ui.swing.ValidationPanel;

//~--- JDK imports ------------------------------------------------------------

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;


/**
 *
 * @author JK
 */
public class ProductEdit extends javax.swing.JDialog {
    private boolean flagSave;
    private Product product;

    // Variables declaration - do not modify                     
    private javax.swing.JButton    closeButton;
    private javax.swing.JLabel     codeLabel;
    private javax.swing.JTextField codeTextField;
    private javax.swing.JSeparator lineSeparator;
    private javax.swing.JLabel     nameLabel;
    private javax.swing.JTextField nameTextField;
    private javax.swing.JLabel     pointsLabel;
    private javax.swing.JTextField pointsTextField;
    private javax.swing.JLabel     priceLabel;
    private javax.swing.JTextField priceTextField;
    private javax.swing.JPanel     productEditPanel;
    private javax.swing.JLabel     productIdLabel;
    private javax.swing.JLabel     productIdValueLabel;
    private javax.swing.JButton    saveButton;

    /*
     * Creates new form ProductEdit
     * @param parent
     * @param modal
     */

    /**
     *
     * @param parent
     * @param modal
     */
    
    public ProductEdit(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        setMinimumSize(new Dimension(410, 300));    // Integer.MAX_VALUE));
        addComponentListener(new ComponentAdapter() {
            @Override
            public void componentResized(ComponentEvent e) {
                Dimension size = getSize();

                size.width = 240;
                setSize(size);
            }
        });

        final ProductEdit pd = this;

        flagSave = false;
        closeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                flagSave = false;
                pd.dispose();
            }
        });
        saveButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                flagSave = true;
                pd.dispose();
            }
        });

        final ValidationPanel pnl = new ValidationPanel();

        this.add(pnl);

        ValidationGroup group = pnl.getValidationGroup();

        group.add(codeTextField,
                  ValidatorUtils.merge(StringValidators.REQUIRE_NON_EMPTY_STRING,
                                       ValidatorUtils.merge(StringValidators.NO_WHITESPACE)));
        group.add(nameTextField, ValidatorUtils.merge(StringValidators.REQUIRE_NON_EMPTY_STRING));
        group.add(pointsTextField,
                  ValidatorUtils.merge(StringValidators.REQUIRE_NON_EMPTY_STRING,
                                       ValidatorUtils.merge(StringValidators.REQUIRE_VALID_INTEGER)));
        group.add(priceTextField,
                  ValidatorUtils.merge(StringValidators.REQUIRE_NON_EMPTY_STRING,
                                       ValidatorUtils.merge(StringValidators.REQUIRE_NON_NEGATIVE_NUMBER)));
    }

    /**
     *
     * @return
     */
    public boolean getFlagSave() {
        return flagSave;
    }

    /**
     *
     * @return
     */
    public Product getProduct() {
        product = new Product();
        product.setProductId(Integer.parseInt(productIdValueLabel.getText()));
        product.setCode(codeTextField.getText());
        product.setName(nameTextField.getText());
        product.setPrice(Float.parseFloat(priceTextField.getText()));
        product.setPoints(Integer.parseInt(pointsTextField.getText()));

        return product;
    }

    /**
     *
     * @param product
     */
    public void setProduct(Product product) {
        productIdValueLabel.setText(product.getProductId().toString());
        codeTextField.setText(product.getCode());
        nameTextField.setText(product.getName().toString());
        priceTextField.setText(Float.toString(product.getPrice()));
        pointsTextField.setText(Integer.toString(product.getPoints()));
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        productEditPanel    = new javax.swing.JPanel();
        lineSeparator       = new javax.swing.JSeparator();
        closeButton         = new javax.swing.JButton();
        pointsTextField     = new javax.swing.JTextField();
        saveButton          = new javax.swing.JButton();
        codeLabel           = new javax.swing.JLabel();
        productIdLabel      = new javax.swing.JLabel();
        priceTextField      = new javax.swing.JTextField();
        productIdValueLabel = new javax.swing.JLabel();
        pointsLabel         = new javax.swing.JLabel();
        priceLabel          = new javax.swing.JLabel();
        nameLabel           = new javax.swing.JLabel();
        nameTextField       = new javax.swing.JTextField();
        codeTextField       = new javax.swing.JTextField();
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setPreferredSize(new java.awt.Dimension(600, 240));
        productEditPanel.setPreferredSize(new java.awt.Dimension(480, 240));
        closeButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/supermarketgui/icons/button_grey_close.png")));    // NOI18N
        closeButton.setText("�?λείσιμο");
        closeButton.setToolTipText("�?λείσιμο της φ�?�?μας επεξε�?γασίας π�?ο�?�?ντος");
        closeButton.setMaximumSize(new java.awt.Dimension(125, 25));
        closeButton.setMinimumSize(new java.awt.Dimension(125, 25));
        closeButton.setPreferredSize(new java.awt.Dimension(125, 25));
        pointsTextField.setName("Π�?ντοι π�?ο�?�?ντος");                                         // NOI18N
        saveButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/supermarketgui/icons/save.png")));    // NOI18N
        saveButton.setText("Αποθήκευση");
        saveButton.setToolTipText("Αποθήκευση των στοιχείων του π�?ο�?�?ντος");
        saveButton.setMaximumSize(new java.awt.Dimension(125, 25));
        saveButton.setMinimumSize(new java.awt.Dimension(125, 25));
        saveButton.setPreferredSize(new java.awt.Dimension(125, 25));
        saveButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                saveButtonMouseClicked(evt);
            }
        });
        codeLabel.setText("�?ωδικ�?ς");
        productIdLabel.setText("ID Π�?ο�?�?ντος");
        priceTextField.setName("Τιμή π�?ο�?�?ντος");         // NOI18N
        pointsLabel.setText("Π�?ντοι");
        priceLabel.setText("Τιμή");
        nameLabel.setText("Π�?ο�?�?ν");
        nameTextField.setName("�?νομα π�?ο�?�?ντος");        // NOI18N
        codeTextField.setName("�?ωδικ�?ς π�?ο�?�?ντος");    // NOI18N
        codeTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                codeTextFieldActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout productEditPanelLayout = new javax.swing.GroupLayout(productEditPanel);

        productEditPanel.setLayout(productEditPanelLayout);
        productEditPanelLayout.setHorizontalGroup(
            productEditPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                productEditPanelLayout.createSequentialGroup().addContainerGap().addGroup(
                    productEditPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                        productEditPanelLayout.createSequentialGroup().addComponent(
                            lineSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 385,
                            javax.swing.GroupLayout.PREFERRED_SIZE).addGap(0, 0, Short.MAX_VALUE)).addGroup(
                                productEditPanelLayout.createSequentialGroup().addGroup(
                                    productEditPanelLayout.createParallelGroup(
                                        javax.swing.GroupLayout.Alignment.LEADING).addComponent(
                                        productIdLabel).addComponent(codeLabel).addComponent(nameLabel).addComponent(
                                        priceLabel).addComponent(pointsLabel)).addPreferredGap(
                                            javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addGroup(
                                                productEditPanelLayout.createParallelGroup(
                                                    javax.swing.GroupLayout.Alignment.LEADING).addComponent(
                                                        codeTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 61,
                                                            javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(
                                                                productIdValueLabel,
                                                                    javax.swing.GroupLayout.PREFERRED_SIZE, 61,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(
                                                                            priceTextField,
                                                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                    61,
                                                                                    javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(
                                                                                        nameTextField,
                                                                                            javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                                300,
                                                                                                javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(
                                                                                                    pointsTextField,
                                                                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                                            61,
                                                                                                            javax.swing.GroupLayout.PREFERRED_SIZE).addGroup(
                                                                                                                productEditPanelLayout.createSequentialGroup().addComponent(
                                                                                                                    saveButton,
                                                                                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                                                            javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                                                                javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(
                                                                                                                                    javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addComponent(
                                                                                                                                        closeButton,
                                                                                                                                            javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                                                                                103,
                                                                                                                                                javax.swing.GroupLayout.PREFERRED_SIZE))).addContainerGap(
                                                                                                                                                    15,
                                                                                                                                                    Short.MAX_VALUE)))));
        productEditPanelLayout.setVerticalGroup(
            productEditPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                javax.swing.GroupLayout.Alignment.TRAILING,
                productEditPanelLayout.createSequentialGroup().addGap(0, 13, Short.MAX_VALUE).addGroup(
                    productEditPanelLayout.createParallelGroup(
                        javax.swing.GroupLayout.Alignment.LEADING, false).addComponent(
                        productIdLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                        Short.MAX_VALUE).addComponent(
                            productIdValueLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 16,
                            javax.swing.GroupLayout.PREFERRED_SIZE)).addGap(18, 18, 18).addGroup(
                                productEditPanelLayout.createParallelGroup(
                                    javax.swing.GroupLayout.Alignment.BASELINE).addComponent(codeLabel).addComponent(
                                    codeTextField, javax.swing.GroupLayout.PREFERRED_SIZE,
                                    javax.swing.GroupLayout.DEFAULT_SIZE,
                                    javax.swing.GroupLayout.PREFERRED_SIZE)).addGap(18, 18, 18).addGroup(
                                        productEditPanelLayout.createParallelGroup(
                                            javax.swing.GroupLayout.Alignment.BASELINE).addComponent(
                                                nameLabel).addComponent(
                                                    nameTextField, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                            javax.swing.GroupLayout.PREFERRED_SIZE)).addPreferredGap(
                                                                javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addGroup(
                                                                    productEditPanelLayout.createParallelGroup(
                                                                        javax.swing.GroupLayout.Alignment.BASELINE).addComponent(
                                                                            priceLabel).addComponent(
                                                                                priceTextField,
                                                                                    javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                            javax.swing.GroupLayout.PREFERRED_SIZE)).addPreferredGap(
                                                                                                javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addGroup(
                                                                                                    productEditPanelLayout.createParallelGroup(
                                                                                                        javax.swing.GroupLayout.Alignment.BASELINE).addComponent(
                                                                                                            pointsLabel).addComponent(
                                                                                                                pointsTextField,
                                                                                                                    javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                                                            javax.swing.GroupLayout.PREFERRED_SIZE)).addPreferredGap(
                                                                                                                                javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addComponent(
                                                                                                                                    lineSeparator,
                                                                                                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                                                                            3, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(
                                                                                                                                                javax.swing.LayoutStyle.ComponentPlacement.RELATED).addGroup(
                                                                                                                                                    productEditPanelLayout.createParallelGroup(
                                                                                                                                                        javax.swing.GroupLayout.Alignment.BASELINE).addComponent(
                                                                                                                                                            saveButton,
                                                                                                                                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                                                                                                    javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                                                                                                        javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(
                                                                                                                                                                            closeButton,
                                                                                                                                                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                                                                                                                    javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                                                                                                                        javax.swing.GroupLayout.PREFERRED_SIZE)).addContainerGap()));
        getContentPane().add(productEditPanel, java.awt.BorderLayout.PAGE_END);
        pack();
    }    // </editor-fold>//GEN-END:initComponents

    private void codeTextFieldActionPerformed(java.awt.event.ActionEvent evt) {    // GEN-FIRST:event_codeTextFieldActionPerformed

        // TODO add your handling code here:
    }    // GEN-LAST:event_codeTextFieldActionPerformed

    private void saveButtonMouseClicked(java.awt.event.MouseEvent evt) {    // GEN-FIRST:event_saveButtonMouseClicked

        // TODO add your handling code here:
    }    // GEN-LAST:event_saveButtonMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {

        /* Set the Nimbus look and feel */

        // <editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">

        /*
         *  If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());

                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ProductEdit.class.getName()).log(java.util.logging.Level.SEVERE, null,
                                               ex);
        }

        // </editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                ProductEdit dialog = new ProductEdit(new javax.swing.JFrame(), true);

                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // End of variables declaration                   
}


//~ Formatted by Jindent --- http://www.jindent.com
